//
//  ICChatForwardCell.m
//  ICome
//
//  Created by ENN on 16/7/15.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICChatForwardCell.h"
#import "LCBLayoutButton.h"
#import "ICHeaderFile.h"
#define headImageHeight (34*Level)
#define headButtonHeight (20*Level)
@interface ICChatForwardCell ()

@property (nonatomic, weak) UIImageView *headImageView;

@property (nonatomic, weak) UILabel *titleLabel;





@end

@implementation ICChatForwardCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.leftFreeSpace = 60;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"forwarCell";
    ICChatForwardCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[ICChatForwardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    return cell;
}

- (void)setGroup:(ICGroup *)group
{
    _group = group;
    
    self.titleLabel.text = group.gName;
    if([group.photoId hasPrefix:@"icon"])
    {
        self.headImageView.image = [UIImage imageNamedFromiComeSDKBundle:group.photoId];
    }else{
        if([group.gType intValue] == 2 ||[group.gType intValue] == 3)
        {
            NSString *URL = [ICTools getOglImageURL:(self.group.photoId)];
            [UIImage GroupHeadView:self.headImageView withUrlStr:URL withgId:group.gId withSize:CGSizeMake(200,200)];
        }else{
            NSURL *imgURL = [NSURL URLWithString:[ICTools getMinImageURL:(group.photoId)]];
            [self.headImageView sd_setImageWithURL:imgURL placeholderImage:[UIImage imageNamedFromiComeSDKBundle:@"App_personal_headimg"]];
        }
    }
    
    
    
    
    if(self.buttonBool)
    {
        self.rightbutton.hidden = NO;
    }else{
        self.rightbutton.hidden = YES;
    }
    self.rightbutton.selected = self.selectedBool;
    
}



- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat margin = 15;
    
    [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(margin);
        make.centerY.equalTo(self.mas_centerY);
        make.height.and.width.mas_equalTo(headImageHeight);
    }];
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(self.headImageView.mas_right).offset(margin);
        make.right.equalTo(self.mas_right);
    }];
    
    [self.rightbutton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.right.equalTo(self.mas_right);
        make.height.and.width.mas_equalTo(self.height);
    }];

    
}





#pragma mark - Getter

- (UIImageView *)headImageView {
    if (!_headImageView) {
        UIImageView *imageV = [[UIImageView alloc] init];
        [self.contentView addSubview:imageV];
        _headImageView      = imageV;
//        _headImageView.layer.masksToBounds = YES;
//        _headImageView.layer.cornerRadius = headImageHeight / 2;
//        _headImageView.layer.borderWidth = 2;
//        _headImageView.layer.borderColor =  ICRGB(0xe3e3e3).CGColor;
    }
    return _headImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        UILabel *label = [[UILabel alloc] init];
        [self.contentView addSubview:label];
        _titleLabel    = label;
        _titleLabel.textColor = ICTEXTCOLOR_T1;
        _titleLabel.preferredMaxLayoutWidth = 180;
        _titleLabel.font = [UIFont systemFontOfSize:17];
    }
    return _titleLabel;
}

-(LCBLayoutButton *)rightbutton
{
    if(!_rightbutton)
    {
        LCBLayoutButton * button = [[LCBLayoutButton alloc] initWithFrame:CGRectMake(0, 0, self.height, self.height)];
        button.imageSize = CGSizeMake(headButtonHeight, headButtonHeight);
        button.lcbRelayoutType = LCBRelayoutButtonTypeNoFill;

        [self.contentView addSubview:button];
        _rightbutton = button;
        _rightbutton.hidden = YES;
        [_rightbutton setImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_video_unchecked"] forState:UIControlStateNormal];
        [_rightbutton setImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_video_checked"] forState:UIControlStateSelected];
        [_rightbutton addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightbutton;
}


-(void)rightBtnClick:(UIButton *)but{
    if(self.chatForwardCellBlock)
    {
        self.chatForwardCellBlock(but,self.group);
    }
}




@end
